#!/bin/sh
#
# Copyright (C) 2000-2021 Kern Sibbald
# Copyright (C) 2022-2023 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Simple test to check if storage list is correctly handled for the LastBackupedTo policy
#

TestName="store-mngr-LastBackupedTo"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

# Limit per-device max concurrent jobs to 1
$bperl -e 'add_virtual_changer("vDisk1", 5)'
$bperl -e 'add_virtual_changer("vDisk2", 5)'
$bperl -e 'add_virtual_changer("vDisk3", 5)'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "LabelFormat", "Vol", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "CommCompression", "no", "Director")'
$bperl -e 'add_attribute("$conf/bacula-fd.conf", "CommCompression", "no", "FileDaemon")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "CommCompression", "no", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AllowCompression", "no", "Storage")'
sed 's/sparse=yes;//' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf

echo $tmp/f > $tmp/file-list
dd if=/dev/zero of=$tmp/f count=10000

# Add two simple jobs (one of them with store group)
cat <<END_OF_DATA >> $bin/bacula-dir.conf

Job {
  Name = "SingleStoreJob"
  Client = $HOST-fd
  Messages = Standard
  Type = Backup
  Storage = vDisk1
  Pool = Default
  FileSet="Full Set"
}
Job {
  Name = "StoreGroupJob"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1, vDisk2
  Storage Group Policy = LastBackupedTo
  Pool = Default
  FileSet = "Full Set"
}
END_OF_DATA

$bperl -e 'set_global_maximum_concurrent_jobs(10)'

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out  ${cwd}/tmp/log1.out
setdebug level=0 trace=1 dir
setbandwidth limit=500kb/s client
label volume=TestVolume001 storage=File1 pool=Default slot=1 drive=0
label volume=TestVolume002 storage=File2 pool=Default slot=1 drive=0
run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages


run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

list jobs

quit
END_OF_DATA

echo "*********** Run some backups with vDisk1 and vDisk2 *************************"
run_bacula

stop_bacula

# add a new vDisk Storage
sed -i 's/vDisk1, vDisk2/vDisk1, vDisk2, vDisk3/g' $bin/bacula-dir.conf

echo "*********** Run some backups after adding vDisk3 *************************"

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log3.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log1.out
messages

run job=StoreGroupJob level=Full yes
wait
@$out  ${cwd}/tmp/log2.out
messages

run job=StoreGroupJob yes
wait
@$out  ${cwd}/tmp/log3.out

@output /dev/null
list jobs
messages
quit
END_OF_DATA

run_bacula

stop_bacula

#log1.out only contains vDisk1
grep -l vDisk1 ${cwd}/tmp/log1.out
if [ $? -ne 0 ]; then
    print_debug "Could not find vDisk1 in log1"
    estat=1
fi
grep -L vDisk2 ${cwd}/tmp/log1.out
if [ $? -ne 1 ]; then
    print_debug "vDisk2 in log1. Should not be the case"
    estat=1
fi
grep -L vDisk3 ${cwd}/tmp/log1.out
if [ $? -ne 1 ]; then
    print_debug "vDisk3 in log1. Should not be the case"
    estat=1
fi

#log2.out only contains vDisk2
grep -L vDisk1 ${cwd}/tmp/log2.out
if [ $? -ne 1 ]; then
    print_debug "vDisk1 in log2. Should not be the case"
    estat=1
fi

grep -l vDisk2 ${cwd}/tmp/log2.out
if [ $? -ne 0 ]; then
    print_debug "Could not find vDisk2 in log2"
    estat=1
fi
grep -L vDisk3 ${cwd}/tmp/log2.out
if [ $? -ne 1 ]; then
    print_debug "vDisk3 in log2. Should not be the case"
    estat=1
fi

#log3.out only contains vDisk3
grep -L vDisk1 ${cwd}/tmp/log3.out
if [ $? -ne 1 ]; then
    print_debug "vDisk1 in log3. Should not be the case"
    estat=1
fi
grep -L vDisk2 ${cwd}/tmp/log3.out
if [ $? -ne 1 ]; then
    print_debug "vDisk2 in log3. Should not be the case"
    estat=1
fi
grep -l vDisk3 ${cwd}/tmp/log3.out
if [ $? -ne 0 ]; then
    print_debug "Could not find vDisk3 in log3"
    estat=1
fi

end_test
